package yawning.mew.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.HashMap;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import yawning.mew.gui.listener.IMainWindowListener;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@SuppressWarnings("serial")
public class NewToonDialog extends JDialog {

	private IMainWindowListener mainWindow;
	
	private final JPanel contentPanel = new JPanel();
	private JLabel lblCharacter;
	private JTextField txtCharacter;
	private JTextField txtRealm;
	private JComboBox cmbRegion;
	private HashMap<String, String> regions;
	
	//Regions supported
	private String us = "America and Oceanic";
	private String eu = "Europe";
	private String kr = "Korea";
	private String sea = "South East Asia";
	private String tw = "Taiwan";
	
/*	Dialog box cannot be used as a standalone.
	/**
	 * Launch the application.
	 *//*
	public static void main(String[] args) {
		try {
			NewToonDialog dialog = new NewToonDialog(null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * Create the dialog.
	 */
	public NewToonDialog(IMainWindowListener listener) {
		mainWindow = listener;
		regions = new HashMap<String, String>();
		regions.put(us, "us");
		regions.put(eu, "eu");
		regions.put(kr,"kr");
		regions.put(sea,"sea");
		regions.put(tw,"tw");
		setBounds(100, 100, 450, 176);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.DEFAULT_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		{
			lblCharacter = new JLabel("Character Name");
			contentPanel.add(lblCharacter, "1, 1, right, default");
		}
		{
			txtCharacter = new JTextField();
			contentPanel.add(txtCharacter, "2, 1, fill, default");
			txtCharacter.setColumns(10);
		}
		{
			JLabel lblRealmName = new JLabel("Realm Name");
			contentPanel.add(lblRealmName, "1, 3, right, default");
		}
		{
			txtRealm = new JTextField();
			contentPanel.add(txtRealm, "2, 3, fill, top");
			txtRealm.setColumns(10);
		}
		{
			JLabel lblRegion = new JLabel("Region");
			contentPanel.add(lblRegion, "1, 5, right, default");
		}
		{
			cmbRegion = new JComboBox();
			cmbRegion.setModel(new DefaultComboBoxModel(new String[]{us,eu,kr,sea,tw}));
			contentPanel.add(cmbRegion, "2, 5, fill, default");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseReleased(MouseEvent arg0) {
						mainWindow.newCharacter(getCharacterName(), getRealm(), getRegion());
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public String getRealm(){
		return txtRealm.getText();
	}
	
	public String getCharacterName(){
		return txtCharacter.getText();
	}

	public String getRegion(){
		return (String)regions.get(cmbRegion.getSelectedItem());
	}
}
